﻿using NFine.Application.SystemManage;
using NFine.Code;
using NFine.Domain.Entity.BusinessManage;
using NFine.Domain.Entity.SystemManage;
using System.Web.Mvc;
using System.Linq;

namespace NFine.Web.Areas.SystemManage.Controllers
{
    public class UserController : ControllerBase
    {
        private UserApp userApp = new UserApp();
        private UserLogOnApp userLogOnApp = new UserLogOnApp();

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, string keyword)
        {
            var data = new
            {
                rows = userApp.GetList(pagination, keyword),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public JsonResult GetMembersGridJson(Pagination pagination, string pData)
        {
            var data = new
            {
                rows = userApp.GetMembersList(pagination, pData),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Json(data, JsonRequestBehavior.AllowGet); //Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = userApp.GetForm(keyValue);
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMemberJson()
        {
            var keyValue = OperatorProvider.Provider.GetCurrent().UserId;
            var data = userApp.GetForm(keyValue);
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetAllMemberFormJson(string keyValue)
        {
            var data = userApp.GetAllMemberFormJson(keyValue);
            return Content(data.ToJson());
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(UserEntity userEntity, UserLogOnEntity userLogOnEntity, string keyValue)
        {
            userEntity.ReferenceID = OperatorProvider.Provider.GetCurrent().UserId;
            userApp.SubmitForm(userEntity, userLogOnEntity, keyValue);
            return Success("操作成功。");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitMember(UserEntity userEntity)
        {
            var keyValue = OperatorProvider.Provider.GetCurrent().UserId;
            userApp.SubmitMember(userEntity, keyValue);
            return Success("操作成功。");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult Recharge(UserEntity userEntity)
        {
            if (userEntity.F_MemberMoney <= 0)
                return Error("输入金额有误，请确认");
            userApp.Recharge(userEntity);
            return Success("操作成功。");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult FRecharge(UserEntity userEntity)
        {
            if (userEntity.F_MemberMoney <= 0)
                return Error("输入金额有误，请确认");
            userApp.FRecharge(userEntity);
            return Success("操作成功。");
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitMemberForm(UserEntity userEntity, string Goods, UserLogOnEntity userLogOnEntity, D_GoodsEntity goodsEntity, string keyValue)
        {
            userApp.SubmitMemberForm(userEntity, userLogOnEntity, Goods, keyValue);
            return Success("操作成功。");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubTransfer(UserEntity userEntity)
        {
            userApp.SubTransfer(userEntity);
            return Success("操作成功。");
        }
        [HttpPost]
        [HandlerAuthorize]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            userApp.DeleteForm(keyValue);
            return Success("删除成功。");
        }
        [HttpGet]
        public ActionResult RevisePassword()
        {
            return View();
        }
        [HttpGet]
        public ActionResult MemberTransfer()
        {
            return View();
        }
        [HttpGet]
        public ActionResult MembersInfo()
        {
            return View();
        }
        [HttpGet]
        public ActionResult SystemTransfer()
        {
            string type = "";
            type = Request.QueryString["type"];
            ViewBag.type = type;
            return View();
        }
        
       [HttpGet]
        public ActionResult TransferForm()
        {
            return View();
        }
        public ActionResult MemberRecharge(string keyValue)
        {
            var data = userApp.GetForm(keyValue);
            return View(data);
        }
        public ActionResult MemberFRecharge(string keyValue)
        {
            var data = userApp.GetForm(keyValue);
            return View(data);
        }
        [HttpGet]
        public ActionResult MembersIndex()
        {
            return View();
        }
        [HttpGet]
        public ActionResult MembersForm()
        {
            var u = userApp.GetAmcount(OperatorProvider.Provider.GetCurrent().UserId);
            ViewBag.MemberMoney = u.Amount;
            return View();
        }
        [HttpGet]
        public ActionResult MembersUpdate()
        {
            return View();
        }
        [HttpGet]
        public ActionResult MemberReWord()
        {
            return View();
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitRevisePassword(string userPassword, string keyValue)
        {
            userLogOnApp.RevisePassword(userPassword, keyValue);
            return Success("重置密码成功。");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult DisabledAccount(string keyValue)
        {
            UserEntity userEntity = new UserEntity();
            userEntity.F_Id = keyValue;
            userEntity.F_EnabledMark = false;
            userApp.UpdateForm(userEntity);
            return Success("账户禁用成功。");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult EnabledAccount(string keyValue)
        {
            UserEntity userEntity = new UserEntity();
            userEntity.F_Id = keyValue;
            userEntity.F_EnabledMark = true;
            userApp.UpdateForm(userEntity);
            return Success("账户启用成功。");
        }

        [HttpGet]
        public ActionResult Info()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public JsonResult GetUserSalary(Pagination pagination, string keyword)
        {
            var data = new
            {
                rows = userApp.GetUserSalary(pagination, keyword),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public JsonResult GetCompanyAccount(Pagination pagination, string keyword,string beginTime,string endTime,string ChangeType,string IsIncome)
        {
            var data = new
            {
                rows = userApp.GetCompanyAccount(pagination, keyword, beginTime, endTime, ChangeType, IsIncome),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };

            return Json(data, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public JsonResult GetAllCompanyAccount(string keyword, string beginTime, string endTime, string ChangeType, string IsIncome)
        {
            var tt = userApp.GetCompanyAccount(keyword, beginTime, endTime, ChangeType, IsIncome);
            decimal InSum = 0, OutSum = 0, SumAmount=0;
            foreach (var itm in tt)
            {
                if (itm.IsIncome)
                    InSum = InSum + itm.ChangeAmount;
                else
                    OutSum = OutSum + itm.ChangeAmount;
            }
            SumAmount = InSum + OutSum;
            var tbs = new
            {
                InSum = InSum,
                OutSum = OutSum,
                SumAmount= SumAmount
            };
            return Json(tbs, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public JsonResult GetAllMemberAccount(string keyAccount, string keyRealName)
        {
            var tt = userApp.GetAllMemberAccount(keyAccount, keyRealName);
            decimal SumAmount = 0;
            foreach (var itm in tt)
            {
                SumAmount += itm.F_MemberMoney.Value;
            }
            var tbs = new
            {
                SumAmount = SumAmount
            };
            return Json(tbs, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult UserSalary()
        {
            return View();
        }
        [HttpGet]
        public ActionResult CompanyAccount()
        {
            return View();
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult SubmitRePassword(string type, string old, string newpass)
        {
            userApp.SubmitRePassword(type, old, newpass);
            return Success("操作成功。");
        }
        [HttpGet]
        public ActionResult UserAreas(string id = "", string adminsearch = "")
        {
            if (adminsearch == "adminjson" || !OperatorProvider.Provider.GetCurrent().IsMember)   //非管理员
            {
                UserEntity user = null;
                if (string.IsNullOrEmpty(id))
                    user = userApp.FindEntityByFunc(u => string.IsNullOrEmpty(u.ReferenceID) && u.F_IsAdministrator == false);
                else
                    user = userApp.FindEntityByFunc(u => u.F_Account == id);
                if (user != null)
                {
                    var data = userApp.GetAreas(user.F_Id);
                    if (adminsearch == "adminjson")
                        return Json(data, JsonRequestBehavior.AllowGet);
                    return View(data);
                }
                else
                {
                    if (adminsearch == "adminjson")
                        return Json(new { error = "该用户不存在" }, JsonRequestBehavior.AllowGet);
                    return new ContentResult() { Content = "该用户不存在" };
                }
            }
            return new ContentResult() { Content = "非管理员无权限查看" };

        }

        [HttpGet]
        public ActionResult GetUserBaseInfo(string id)
        {
            var user = userApp.FindLastUserByCardid(id);
            if (user != null)
            {
                var uname = user.F_Account.Substring(0, 4);
                int num = 0;
                if (int.TryParse(user.F_Account.Substring(4, 2), out num))
                {
                    num++;
                    user.F_Account = uname + num.ToString().PadLeft(2, '0');
                } else
                    user.F_Account = "";
            }
            return Json(user, JsonRequestBehavior.AllowGet);
        }
    }
}
